# -*- coding: utf-8 -*-
""" 
@author: ertao.xyz
@create: 2021-12-16 11:16
@description: 
"""
from typing import List, Optional, Any
from pydantic import Field
from tortoise.transactions import atomic
from fastapi import APIRouter, Depends, status, HTTPException, Request, Body, Path, Query
from utils.response_code import ResultResponse, HttpStatus
from auth.auth_casbin import Authority
from utils import crud

from . import model, schema

router = APIRouter()


#
#
# @router.get('/dataoke/',
#             summary='获取大淘客列表',
#             name='获取大淘客列表',
#             response_model=ResultResponse[List[schema.DataokeOut]],
#             dependencies=[Depends(Authority('dataoke,read'))])
# async def get_dataoke_list(page: int = 1, pageSize: int = 20, name: Optional[str] = None):
#     query = model.Dataoke.all()
#     if name is not None:
#         query = query.filter(name__icontains=name)
#     query = await query.offset(page - 1).limit(pageSize)
#     # user_list = await crud.get_user_list()
#     return ResultResponse[List[schema.DataokeOut]](result=query)
#
#
# @router.post('/dataoke/',
#              summary='添加大淘客',
#              name='添加大淘客',
#              response_model=ResultResponse[schema.DataokeOut],
#              dependencies=[Depends(Authority('dataoke,add'))])
# async def add_dataoke(data: schema.DataokeIn):
#     if await crud.has_obj_by_name(model.Dataoke, data.name):
#         return ResultResponse[str](code=HttpStatus.HTTP_601_ROLE_EXIST, message='角色已存在')
#     obj = await crud.create_obj(model.Dataoke, data)
#     return ResultResponse[schema.DataokeOut](result=obj)


# @router.delete('/dataoke/',
#                summary='删除大淘客',
#                name='删除大淘客',
#                response_model=ResultResponse[str],
#                dependencies=[Depends(Authority('dataoke,del'))])
# async def del_dataoke(request: Request, ids: List[int]):
#     result = await crud.delete_obj_by_ids(model.Dataoke, ids)
#     if not result:
#         return ResultResponse[str](code=HttpStatus.HTTP_600_ROLE_NOT_EXIST, message='大淘客不存在')
#     return ResultResponse[str](message='删除成功')


@router.post('/dataoke/{appid}',
             summary='更新大淘客',
             name='更新大淘客',
             response_model=ResultResponse[schema.DataokeOut],
             dependencies=[Depends(Authority('dataoke,update'))])
async def update_dataoke(data: schema.DataokeIn, appid: str = Path(None, max_length=20)):
    obj = await model.Dataoke.update_or_create(defaults=data, wechatMini=appid)
    return ResultResponse[schema.DataokeOut](result=obj)


@router.get('/dataoke/{appid}',
            summary='获取单个大淘客配置',
            name='获取单个大淘客配置',
            response_model=ResultResponse[schema.DataokeOut],
            dependencies=[Depends(Authority('dataoke,update'))])
async def get_dataoke_one(appid: str = Path(None, max_length=20)):
    obj = await model.Dataoke.filter(wechatMini=appid).first()
    if obj:
        return ResultResponse[schema.DataokeOut](result=obj)
    return ResultResponse(result={}, code=HttpStatus.HTTP_422_ROLE_NOT_EXIST)
